package com.example.jin.firstanko

import android.content.DialogInterface
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.Gravity
import android.view.View
import kotlinx.coroutines.experimental.Deferred
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.async
import org.jetbrains.anko.*
import org.jetbrains.anko.coroutines.experimental.bg
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.support.v4.onPageChangeListener
import org.jetbrains.anko.support.v4.viewPager

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MainActivityUI().setContentView(this)

    }

    class MainActivityUI : AnkoComponent<MainActivity> {

        override fun createView(ui: AnkoContext<MainActivity>) = ui.apply {
            verticalLayout {
                padding = dip(5)
                backgroundColor = R.color.colorAccent
                button("good") {
                    async(UI) {
                        val t1 = System.currentTimeMillis();
                        val data: Deferred<Long> = bg {
                            Thread.sleep(20000)
                            val t2 = System.currentTimeMillis()
                            return@bg t2
                        }
                        println(((data.await() - t1) / 1000).toString() + "s")
                        longToast(((data.await() - t1) / 1000).toString() + "s")
                    }
                }
                themedButton("theme", theme = R.style.AppTheme){
                    onClick {
                        val countries= listOf<String>("USA","China","Japan","Russia","Korea")
                       selector("where are from?",countries){
                           _, i -> toast("really${countries[i]}")
                       }
                    }
                }
                button {
                    onClick {
                        alert("今天吃药了吗?") {
                            yesButton {
                                positiveButton("",onClicked={
                                    object :DialogInterface{
                                        override fun cancel() {

                                        }

                                        override fun dismiss() {

                                        }
                                    }
                                })
                                toast("吃了") }
                            noButton { toast("没吃") }
                        }.show()
                    }
                }
                editText {
                    hint = "name"
                    textSize = 24f
                }
                editText {
                    hint = "password"
                    textSize = 24f
                    backgroundColor = R.color.colorAccent
                }.lparams(width = wrapContent, height = dip(10)) {
                    horizontalMargin = dip(4)
                    gravity = Gravity.CENTER
                }
                button("login") {
                    textSize = 26f
                    onClick { ctx.toast("hello") }
                }
                val ID_OK = 1000
                relativeLayout {
                    val bu = button("ok") {
                        id = ID_OK
                    }.lparams { alignParentTop() }
                    button("cancel").lparams {
                        below(bu)
                    }
                }
                include<View>(R.layout.test1) {
                    backgroundColor = R.color.colorPrimaryDark
                }.lparams(width = matchParent, height = 200)
                viewPager {
                    onPageChangeListener {
                        onPageScrollStateChanged { }
                        onPageSelected { }
                    }
                }
            }

        }.view

    }
}
